package com.darren.euc.util.sms;

import com.alibaba.fastjson.JSONObject;
import com.darren.euc.action.ClientAction;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 发送验证码工具类
 * Created by hanji on 2018/3/12.
 */
public class SendVerifyCode {
    private static final Logger _log = Logger.getLogger(SendVerifyCode.class);
    private static final String USER_ID = "200252";
    private static final String USER_PWD = "716ecff70277b4b0d2a7f78fccc37ebe";
    private static final String LINK_ADDRESS = "http://112.74.139.4:8002/sms3_api/jsonapi/jsonrpc2.jsp";
    private static String PARAM_JSON = "{\"id\":1,\"method\":\"send\",\"params\":{\"userid\":\"USERID\",\"password\":\"PASSWORD\",\"submit\":[{content:\"[\\u6849\\u6811IBC]\\u9a8c\\u8bc1\\u7801CODE\\uff0c\\u60a8\\u6b63\\u5728\\u8fdb\\u884c\\u8eab\\u4efd\\u9a8c\\u8bc1\\uff0c\\u6253\\u6b7b\\u90fd\\u4e0d\\u8981\\u544a\\u8bc9\\u522b\\u4eba\\u54e6\\uff01\",\"phone\":\"PHONE\"}]}}";
    /**
     * 发送验证码
     */
    private static int sendCode(String code,String mobile){
        String param = PARAM_JSON.replaceAll("USERID",USER_ID).replaceAll("PASSWORD",USER_PWD).replaceAll("CODE",code).replaceAll("PHONE",mobile);
        try {
            String result = HttpUtils.doPost(LINK_ADDRESS,param);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject.get("id").equals(1)){
                return 1;
            }
            return jsonObject.getInteger("id").equals(1) ? 1 : -1;
        } catch (Exception e) {
            _log.error(e.getMessage());
            return -1;
        }
    }

    public static void main(String [] args){
        String code = RandomStrUtil.getSixNumberRand();
        int status = sendCode(code,"18515190967");
        if(status > 0){
            _log.info("发送成功！");
        }
    }

    //-----------------------------------

    private static LoadingCache<Object, Object> cache = CacheBuilder.newBuilder().maximumSize(1000)
            .expireAfterWrite(60, TimeUnit.SECONDS).recordStats().build(new CacheLoader<Object, Object>() {
                @Override
                public Object load(Object key) throws Exception {
                    return key;
                }
            });

    public static Object get(Object key) throws ExecutionException {
        Object var = cache.get(key);
        if (var.equals(key)) {
            return null;
        } else {
            return cache.get(key);
        }
    }

    public static void put(Object key, Object value) {
        cache.put(key, value);
    }

    public static void sendCode(String phone) throws Exception {
        String code = RandomStrUtil.getSixNumberRand();
        int status = sendCode(code, phone);
        if (status > 0){
            put(phone, code);
        }else {
            throw new Exception("发送失败！");
        }
    }

    public static boolean checkCode(String phone, String code){
        String _code = null;
        try {
            _code = (String) get(phone);
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        if (_code != null && _code.equals(code))
            return true;
        return false;
    }

}
